#include "cmsis_os2.h"
#include "lvgl.h"


extern osMutexId_t LvglMutexHandle;

lv_obj_t* chart;
lv_obj_t* chart1;

static void add_data(lv_timer_t* timer)
{
    LV_UNUSED(timer);
    lv_chart_set_next_value(chart, timer->user_data, lv_rand(20, 90));
}

static void add_data1(lv_timer_t* timer)
{
    LV_UNUSED(timer);
    lv_chart_set_next_value(chart1, timer->user_data, lv_rand(20, 90));
}

void StartLvglTask(void *argument)
{
    osMutexAcquire(LvglMutexHandle, osWaitForever);

    chart = lv_chart_create(lv_scr_act());                              /* 创建图表部件 */
    lv_obj_set_size(chart, 200, 150);                                   /* 设置图表大小 */
    lv_obj_center(chart);                                               /* 中间对齐 */
    /* 添加数据序列 */
    lv_chart_series_t* ser1 = NULL;
    ser1 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_SECONDARY_X);
    lv_chart_set_update_mode(chart, LV_CHART_UPDATE_MODE_SHIFT);        /* 将旧数据移到左边 */
    lv_timer_create(add_data, 200, ser1);                               /* 创建定时器 */
    lv_chart_refresh(chart);                                            /* 更新图表 */

    chart1 = lv_chart_create(lv_scr_act());                             /* 创建图表部件1 */
    lv_obj_set_size(chart1, 200, 150);                                  /* 设置图表大小 */
    lv_obj_align_to(chart1, chart,LV_ALIGN_OUT_RIGHT_MID,0,20);         /* 对齐 */
    /* 添加数据序列 */
    lv_chart_series_t* ser2 = NULL;
    ser2 = lv_chart_add_series(chart1, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_SECONDARY_X);
    lv_chart_set_update_mode(chart1, LV_CHART_UPDATE_MODE_CIRCULAR);    /* 以循环的方式添加新数据 */
    lv_timer_create(add_data1, 200, ser2);                              /* 创建定时器 */
    lv_chart_refresh(chart1);                                           /* 更新图表 */

    osMutexRelease(LvglMutexHandle);

    for(;;)
    {
        osMutexAcquire(LvglMutexHandle, osWaitForever);
        lv_timer_handler();
        osMutexRelease(LvglMutexHandle);
        osDelay(5);
    }
}
